<?php
class persediaan_model extends CI_Model
{
	
	//header
	private $NoFaktur;
    private $DateFaktur;
    private $TotalQuantity;
    private $TotalAmount; 
	private $Keterangan;
    private $createdby;
    private $createdon;
    private $lastupdateby;
  	private $lastupdateon;
  	private $active;
	//detail
	private $IDdetail;
	private $NoPo;
    private $KodeDetailBarang;
    private $Quantity;
    private $Discount; 
	private $Ppn;
    private $Amount;
	
	//header
	function setNoFaktur($nmfktr){
		$this->NoFaktur = $nmfktr;
	}
	function getNoFaktur(){
		return $this->NoFaktur;
	}
	
	function setDateFaktur($datefktr){
		$this->DateFaktur = $datefktr;
	}
	function getDateFaktur(){
		return $this->DateFaktur;
	}
	
	function setTotalQuantity($Qtot){
		$this->TotalQuantity = $Qtot;
	}
	function getTotalQuantity(){
		return $this->TotalQuantity;
	}
	
	function setTotalAmount($TAmount){
		$this->TotalAmount = $TAmount;
	}
	function getTotalAmount(){
		return $this->TotalAmount;
	}
	
	function setKeterangan($ket){
		$this->Keterangan = $ket;
	}
	function getKeterangan(){
		return $this->Keterangan;
	}
	
	function setCreatedBy($c_by){
		$this->createdby = $c_by;
	}
	function getCreatedBy(){
		return $this->createdby;
	}

	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->createdon = $dateNow;
	}
	function getCreatedOn(){
		return $this->createdon;
	}

	function setLastUpdateBy($u_by){
		$this->lastupdateby = $u_by;
	}
	function getLastUpdateBy(){
		return $this->lastupdateby;
	}

	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->lastupdateon = $dateNow;
	}
	function getLastUpdateOn(){
		return $this->lastupdateon;
	}

	function setActive($active_val){
		$this->active = $active_val;
	}

	function getActive(){
		return $this->active;
	}
	//end header
	
	//detail
	function setIDdetail($IDdetail){
		$this->IDdetail = $IDdetail;
	}
	function getIDdetail(){
		return $this->IDdetail;
	}
	
	function setNoPo($nopo){
		$this->NoPo = $nopo;
	}
	function getNoPo(){
		return $this->NoPo;
	}
	
	function setKodeDetailBarang($kdDetBrng){
		$this->KodeDetailBarang = $kdDetBrng;
	}
	function getKodeDetailBarang(){
		return $this->KodeDetailBarang;
	}
	
	function setQuantity($qty){
		$this->Quantity = $qty;
	}
	function getQuantity(){
		return $this->Quantity;
	}
	
	function setDiscount($disc){
		$this->Discount = $disc;
	}
	function getDiscount(){
		return $this->Discount;
	}
	
	function setPpn($ppn){
		$this->Ppn = $ppn;
	}
	function getPpn(){
		return $this->Ppn;
	}
	
	function setAmount($amount){
		$this->Amount = $amount;
	}
	function getAmount(){
		return $this->Amount;
	}
	
	function __construct(){
		parent::__construct();
	}
	
	function getPersediaanAll()
	{
		//$this->db->select()->where(array('active'=>'1'));
		$query = $this->db->get('faktur_product_pembelian');
		return $query->result();
	}
	
	function getdetailid()
	{
		$query = $this->db->query("select MAX(IDdetail) as kddetailid from transaction_faktur_product_pembelian");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $detailid)
			{
				$kd = ((int)$detailid->kddetailid)+1;
			}
			
		}
		else
		{
			$kd = "1";
		}
		
		return $kd;
	}
	
	function getFakturNo()
	{
		$query = $this->db->query("select MAX(RIGHT(NoFaktur,4)) as nofktr from faktur_product_pembelian");
		$nf = "";
		if($query->num_rows()>0)
		{
			foreach($query->result() as $idsupp)
			{
				$tmpsupp = ((int)$idsupp->nofktr)+1;
				$nf = sprintf("%04s", $tmpsupp);
			}
		}
		else
		{
			$nf = "0001";
		}
		
		$nf = "FK".$nf; 
		
		return $nf;
	}

	function getnpo()
	{
		//$query = $this->db->get_where('order_product', array('active' => '1'));
		//return $query->result();
		
		$query = $this->db->query(" SELECT * FROM order_product WHERE NoPo NOT IN ( SELECT NoPo FROM transaction_faktur_product_pembelian) AND Active='1'");
		
		return $query->result();
	}
	
	function cekData($nopo)
	{
		$query = $this->db->get_where('transaction_faktur_product_pembelian', array('NoPo' => $nopo));
		return $query->num_rows();
	}
	
	function manualQuery($q)
	{
		return $this->db->query($q);
	}
	
	function insertFakturHeader(){
		$query = $this->db->query("select MAX(RIGHT(NoFaktur,4)) as nofaktur from faktur_product_pembelian");
		$kd = "";
		if($query->num_rows()>0)
		{
			foreach($query->result() as $idsupp)
			{
				$tmpsupp = ((int)$idsupp->nofaktur)+1;
				$kd = sprintf("%04s", $tmpsupp);
			}
		}
		else
		{
			$kd = "0001";
		}
			
		$kdsuppok="FK".$kd;
		$this->setNoFaktur($kdsuppok);
		
		$data = array(
			'NoFaktur' => $this->getNoFaktur(),
			'DateFaktur' => $this->getDateFaktur(),
			'TotalQuantity' => $this->getTotalQuantity(),
			'TotalAmount' => $this->getTotalAmount(),
			'Keterangan' => $this->getKeterangan(),
			'Createdby' => $this->getCreatedBy(),
			'Createdon' => $this->getCreatedOn(),
			'Lastupdateby' => $this->getLastupdateby(),
			'Lastupdateon' => $this->getLastupdateon(),
			'Active' => '1'					
		);
		
		$this->db->insert('faktur_product_pembelian',$data);
	}
	
	function insertFakturDetail(){	
		$data = array(
			'NoFaktur' => $this->getNoFaktur(),
			'IDdetail' => $this->getIDdetail(),
			'NoPo' => $this->getNoPo(),
			'KodeDetailBarang' => $this->getKodeDetailBarang(),
			'Quantity' => $this->getQuantity(),
			'Discount' => $this->getDiscount(),
			'Ppn' => $this->getPpn(),
			'Amount' => $this->getAmount(),
			'Active' => '1'		
		);
		
		$this->db->insert('transaction_faktur_product_pembelian',$data);
	}
}
?>